// pages/invoiceHis/invoiceHis.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseCount:0,
    price:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    //获取手机高度
    that.setData({
      navH: app.globalData.navHeight,
      price:0,
      chooseCount:0
    });
    wx.request({
      url: app.globalData.url + "getOrderListForInvoice",
      method: "GET",
      data: {
        userId: wx.getStorageSync("userId")
      },
      success: function (res) {
        var data = res.data.data;
        var chooses = [];
        for(var i=0;i<res.data.data.length;i++){
          chooses[i] = false;
        }
        if (res.data.result == 1) {
          that.setData({
            invoiceList: data,
            chooses:chooses,
            chooseAll:false
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  choose: function (e) {
    var count = this.data.chooseCount;
    var price = this.data.price;
    if (this.data.chooses[e.currentTarget.dataset.index]){
      this.data.chooses[e.currentTarget.dataset.index] = false;
      count--;
      price = price - e.currentTarget.dataset.price;
    }else{
      this.data.chooses[e.currentTarget.dataset.index] = true;
      count++;
      price = price + e.currentTarget.dataset.price;
    }
    var chooseAll = true;
    for (var i = 0; i < this.data.chooses.length; i++) {
      if (this.data.chooses[i] == false) {
        chooseAll = false;
      }
    }
    price = parseFloat(price.toFixed(2));
    this.setData({
      chooses:this.data.chooses,
      chooseCount:count,
      price:price,
      chooseAll:chooseAll
    })
    
  },
  chooseAll: function () {
    if(this.data.chooseAll == false){
      var chooseAll = true;
      var price = 0;
      var chooseCount = this.data.chooses.length;
      for (var i = 0; i < this.data.chooses.length; i++) {
        this.data.chooses[i] = true;
        price = price + this.data.invoiceList[i].orderPrice;
      }
      price = parseFloat(price.toFixed(2));
      this.setData({
        chooses: this.data.chooses,
        chooseCount: chooseCount,
        price: price,
        chooseAll: chooseAll
      })
    }else{
      for (var i = 0; i < this.data.chooses.length; i++) {
        this.data.chooses[i] = false;
      }
      this.setData({
        chooses: this.data.chooses,
        chooseCount: 0,
        price: 0,
        chooseAll: false
      })
    }
   
  },
  goNext:function(){
    if (this.data.price>0){
      var orderList = "";
      for (var i = 0; i < this.data.chooses.length; i++) {
        if (this.data.chooses[i]){
          orderList += this.data.invoiceList[i].orderId+',';
          }
      }
      orderList = orderList.substring(0,orderList.length-1);
      wx.navigateTo({
        url: '/pages/invoicingTwo/invoicingTwo?price=' + this.data.price + '&orderList=' + orderList
      })
    }else{
      wx.showToast({
        title: '请选择开发票订单',
        icon:'none'
      })
    }
  },
  navBack: function () {
    wx.navigateBack({

    })
  },
})